const model = require('./model')
const {
  SUCCESS_RESPONSE,
  FAIL_RESPONSE,
  PORT,
  HOST,
} = require('./config/constant')
const initModels = require('./models/init-models')
const sequelize = require('./sequelize')
const { Op } = require('sequelize')
const marked = require('marked')

const models = initModels(sequelize)

// 搜索获取文章列表
exports.getArticleByse = async (req, res) => {
  let { searchData } = req.query
  let result = await models.article.findAll({
    where: {
      title: {
        [Op.like]: `%${searchData}%`, // 查询包含"张"的名字
      },
    },
    attributes: ['article_id', 'cover', 'title', 'intro'],
    raw: true,
  })
  res.json(SUCCESS_RESPONSE('获取文章数据成功', result))
}

// 通过分类id获取分类名称
exports.getCategoryNameById = async (req, res) => {
  let category_id = req.query.category_id
  let result = await models.category.findAll({
    where: { category_id: category_id },
    attributes: ['category_name'],
    raw: true,
  })
  res.json(SUCCESS_RESPONSE('获取分类名称数据成功', result[0].category_name))
}

// 通过文章id获取文章详情数据
exports.getArticleDetailById = async (req, res) => {
  let article_id = req.query.article_id
  let result = await models.article.findAll({
    attributes: ['cover', 'title', 'category_id', 'intro', 'content'],
    where: { article_id: article_id },
    raw: true,
  })
  result[0].content = marked.parse(result[0].content)
  res.json(SUCCESS_RESPONSE('获取文章详情数据成功', result[0]))
}

// 通过分类获取文章列表数据
exports.getArticleListBycate = async (req, res) => {
  let category_id = req.query.category_id
  console.log(typeof category_id)

  const whereCondition = {}
  if (category_id && category_id !== 'undefined') {
    whereCondition.category_id = category_id
  }
  let result = await models.article.findAll({
    where: whereCondition,
    attributes: ['article_id', 'cover', 'title', 'intro'],
    raw: true,
  })
  res.json(SUCCESS_RESPONSE('获取文章列表数据成功', result))
}

// 获取分类名称
exports.getCategoryName = async (req, res) => {
  let result = await models.category.findAll({
    attributes: ['category_id', 'category_name'],
    raw: true,
  })
  res.json(SUCCESS_RESPONSE('获取分类名称数据成功', result))
}

// 获取文章列表数据
exports.getArticleList = async (req, res) => {
  let result = await models.article.findAll({
    attributes: ['article_id', 'cover', 'title', 'intro'],
    raw: true,
  })
  res.json(SUCCESS_RESPONSE('获取文章列表数据成功', result))
}

// 获取推荐文章数据
exports.getGoodArticle = async (req, res) => {
  let result = await models.article.findAll({
    where: {
      good_count: {
        [Op.gt]: 5,
      },
    },
    attributes: ['article_id', 'cover', 'title', 'intro'],
    raw: true,
  })
  res.json(SUCCESS_RESPONSE('获取文章列表数据成功', result))
}

/* -----------------------------------------------------以上为小程序端----------------------------------------------------- */

// 增加点赞
exports.addGoodData = async (req, res) => {
  let { article_id, newGood } = req.body
  await model.addGood(article_id, newGood)
  res.json(SUCCESS_RESPONSE('增加成功'))
}

// 增加浏览量
exports.addViewsData = async (req, res) => {
  let { article_id, newViews } = req.body
  await model.addViews(article_id, newViews)
  res.json(SUCCESS_RESPONSE('增加成功'))
}

// 获得具体文章数据
exports.getArticleDetailData = async (req, res) => {
  let { article_id } = req.query
  let result = await model.getArticleDetail(article_id)
  res.json(SUCCESS_RESPONSE('获取成功', result))
}

// 获取文章数据
exports.getArticleData = async (req, res) => {
  let { page, pagesize, category_id, tag_id } = req.query
  let result = await model.getArticle(page, pagesize, category_id, tag_id)
  let count = await model.getCount(category_id, tag_id)
  res.json(SUCCESS_RESPONSE('获取文章成功', { result, count }))
}

// 获取标签数据
exports.getTagData = async (req, res) => {
  let result = await model.getTag()
  res.json(SUCCESS_RESPONSE('获取分类成功', result))
}

// 获取分类数据
exports.getCategoryData = async (req, res) => {
  let result = await model.getCategory()
  res.json(SUCCESS_RESPONSE('获取分类成功', result))
}
